﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web.UI.WebControls;
using Ninject;
using Legazpi.Payroll.Web.IoC;
using Legazpi.Payroll.Services.Services;
using System.Reflection;
using System.ComponentModel;
using Emz.Core.Models.Enums;

namespace Legazpi.Payroll.Web.Helpers
{
    public static class DropDownExtensions
    {

        public static void ToPositionSelectList(this DropDownList dropdown, int selectedValue)
        {        
            var kernel = new StandardKernel(new PayrollModule());
            var  _positionService = kernel.Get<PositionService>();

            dropdown.Items.Clear();
            dropdown.Items.Add(new ListItem { Value = "0", Text = "--Select--" });
            dropdown.Items.AddRange(
                  _positionService.GetAll()
                  .Select(x =>
                          new ListItem
                          {
                              Value = x.Id.ToString(),
                              Text = x.Code,
                              Selected = x.Id == selectedValue
                          }).ToArray<ListItem>()
                 );
        }

        public static void ToClientSelectList(this DropDownList dropdown, int selectedValue)
        {
            var kernel = new StandardKernel(new PayrollModule());
            var _clientService = kernel.Get<ClientService>();

            dropdown.Items.Clear();
            dropdown.Items.Add(new ListItem { Value = "0", Text = "--Select--" });
            dropdown.Items.AddRange(
                  _clientService.GetAll()
                  .Select(x =>
                          new ListItem
                          {
                              Value = x.Id.ToString(),
                              Text = x.Code,
                              Selected = x.Id == selectedValue
                          }).ToArray<ListItem>()
                 );
        }

        public static void ToSectorSelectList(this DropDownList dropdown, int selectedValue)
        {
            var kernel = new StandardKernel(new PayrollModule());
            var _sectorService = kernel.Get<SectorService>();

            dropdown.Items.Clear();
            dropdown.Items.Add(new ListItem { Value = "0", Text = "--Select--" });
            dropdown.Items.AddRange(
                  _sectorService.GetAll()
                  .Select(x =>
                          new ListItem
                          {
                              Value = x.Id.ToString(),
                              Text = x.Name,
                              Selected = x.Id == selectedValue
                          }).ToArray<ListItem>()
                 );
        }

        public static void ToSuperVisorSelectList(this DropDownList dropdown, int selectedValue)
        {
            dropdown.Items.Clear();
            dropdown.Items.Add(new ListItem { Value = "0", Text = "--Select--" });            
            dropdown.Items.AddRange(
                  Helper.GetSupervisors()
                  .Select(x =>
                          new ListItem
                          {
                              Value = x.Id.ToString(),
                              Text = x.Fullname,
                              Selected = x.Id == selectedValue
                          }).ToArray<ListItem>()
                 );
        }

        public static void ToAdjAccountSignSelectList(this DropDownList dropdown, int selectedValue)
        {
            dropdown.Items.Clear();
            dropdown.Items.Add(new ListItem { Value = "0", Text = "minus(-)", Selected = selectedValue == 0 });
            dropdown.Items.Add(new ListItem { Value = "1", Text = "add(+)", Selected = selectedValue == 1 });
        }

        public static void ToEmployeeStatusSelectList(this DropDownList dropdown, int selectedValue)
        {
            dropdown.Items.Clear();
          
            var source = Enum.GetValues(typeof(EmployedStatus));
            var descriptionAttribute = typeof(DescriptionAttribute);
            
            foreach (var value in source)
            {
                FieldInfo field = value.GetType().GetField(value.ToString());
                var attrs = (DescriptionAttribute)field.GetCustomAttributes(descriptionAttribute, false).FirstOrDefault();
                dropdown.Items.Add(new ListItem 
                {
                    Value = ((int)value).ToString(),
                    Text = attrs != null ? attrs.Description : value.ToString(),
                    Selected = (int)value == selectedValue
                });                                               
            }
        }

        public static void ToAdjAccountTypeSelectList(this DropDownList dropdown,int selectedValue)
        {
            dropdown.Items.Clear();

            var source = Enum.GetValues(typeof(AccountType));
            var descriptionAttribute = typeof(DescriptionAttribute);

            foreach (var value in source)
            {
                FieldInfo field = value.GetType().GetField(value.ToString());
                var attrs = (DescriptionAttribute)field.GetCustomAttributes(descriptionAttribute, false).FirstOrDefault();
                dropdown.Items.Add(new ListItem
                {
                    Value = ((int)value).ToString(),
                    Text = attrs != null ? attrs.Description : value.ToString(),
                    Selected = (int)value == selectedValue
                });
            }
        }

    }

}
